package com.itheima.controller;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.CheckPageQueryDTO;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/checkgroup")
@Api(tags = "检查组相关接口")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增检查组")
    public Result add (@RequestBody CheckGroup checkGroup, @RequestParam List<Integer> checkitemIds) {
        log.info("新增检查组:{}",checkGroup);
        checkGroupService.add(checkGroup,checkitemIds);
        return Result.success();
    }

    /**
     * 检查组批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("检查组批量删除")
    public Result delete (@RequestParam List<Long> ids) {
        log.info("检查组批量删除");
        checkGroupService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据套餐来查询检查组
     * @param setmealId
     * @return
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    @ApiOperation("根据套餐来查询检查组")
    public Result<List<Integer>> setmealByGroupId (@RequestParam Integer setmealId) {
        List<Integer> ids = checkGroupService.setmealByGroupId(setmealId);
        return Result.success(ids);
    }

    /**
     * 检查组分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation("检查组分页查询")
    public Result<PageResult> page(@RequestBody QueryPageBean queryPageBean){
        log.info("检查组分页查询：{}", queryPageBean);
        PageResult pageResult = checkGroupService.pageQuery(queryPageBean);
        return Result.success(pageResult);
    }

    /**
     * 修改检查组
     * @param checkGroup
     * @return
     */
    @PutMapping("/edit")
    @ApiOperation("修改检查组")
    public Result update (@RequestBody CheckGroup checkGroup, @RequestParam List<Integer> checkitemIds) {
        log.info("修改检查组:{}",checkGroup);
        checkGroupService.updateById(checkGroup,checkitemIds);
        return Result.success();
    }

    /**
     * 全部检查组查询
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("全部检查组查询")
    public Result<List<CheckGroup>> findAll () {
        log.info("全部检查组查询");
        List<CheckGroup> checkGroup = checkGroupService.findAll();
        return Result.success(checkGroup);
    }

    /**
     * 根据主键ID进行检查组查询
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @ApiOperation("根据主键ID进行检查组查询")
    public Result<CheckGroup> geyById (@PathVariable Long id) {
        log.info("根据主键ID进行检查组查询：{}",id);
        CheckGroup checkGroup = checkGroupService.geyById(id);
        return Result.success(checkGroup);
    }

}
